package com.lys.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lys.commonutils.R;
import com.lys.eduservice.entity.Chapter.ChapterVo;
import com.lys.eduservice.entity.EduChapter;
import com.lys.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author lys
 * @since 2022-06-30
 */
@RestController
@Slf4j
@RequestMapping("/eduservice/educhapter")
@Api(tags = "章节管理")
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    /**
     * 根据id查询课程的章节和小结列表
     * @param id
     * @return
     */
    @ApiOperation(value = "查询课程对应的章节和小结接口")
    @GetMapping("findAllChapterAndVideoById/{id}")
    public R findAllChapterAndVideoById(@PathVariable String id){
        List<ChapterVo> chapterVoList = eduChapterService.getAllChapterAndVideoById(id);
        return R.ok().data("chapterVoList",chapterVoList);
    }

    //添加章节信息
    @PostMapping("/addChapter")
    @ApiOperation(value = "添加章节接口")
    public R addChapter(@RequestBody EduChapter eduChapter){
        boolean flag = eduChapterService.save(eduChapter);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //根据chapterId查询章节信息
    @GetMapping("/findChapterById/{chapterId}")
    @ApiOperation(value = "根据chapterId查询章节接口")
    public R findChapterById(@PathVariable String chapterId){
        LambdaQueryWrapper<EduChapter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduChapter::getId,chapterId);
        EduChapter eduChapter = eduChapterService.getOne(queryWrapper);
        return R.ok().data("eduChapterList",eduChapter);
    }

    //修改章节信息
    @PostMapping("/updateChpaterById")
    @ApiOperation(value = "修改章节信息接口")
    public R updateChpaterById(@RequestBody EduChapter eduChapter){
        boolean flag = eduChapterService.updateById(eduChapter);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //删除章节信息
    @DeleteMapping("/deleteChapterById/{chapterId}")
    @ApiOperation(value = "删除章节信息接口")
    public R deleteChapterById(@PathVariable String chapterId){
        boolean flag = eduChapterService.deleteChapterInfoById(chapterId);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

}

